#! /usr/bin/perl -w
use strict;

my $delfile = "rm";
my $scriptName = "HeadGenerator";

my $fn;
my $tfn;
my $iscr = 0;
my $smod;
my $guard;
($fn, $smod) = @ARGV;
if(!defined($fn)){
    $fn = <STDIN>;
    chomp $fn;
}
print "$scriptName Opening...... $fn\n";
if(-e $fn){
    print "File Exists\n";
    $tfn = $fn.'.tmp';
    $iscr = 1;
    rename $fn, $tfn;
    open FH, '>'.$fn || die "I can not open target file";
}
else{
    $iscr = 0;
    open FH, '>'.$fn || die "I can not open target file";
}
print "$scriptName Opened!        $fn\n";

#生成guard
$guard = uc $fn;
$guard =~ s(\\)(/)g;
$guard =~ s(^.*/)()g;
$guard =~ s/\./_/g;
$guard .= '_INCLUDED';
print "Include Guard: $guard\n";

print FH "#ifndef $guard\n";
print FH "#define $guard\n";

if($iscr){
    print FH "\n\n";
    open  FTH, $tfn || die "I cant open tmp file";
    while(<FTH>){
        print FH;
    }
    close FTH;
    system($delfile, $tfn);
}
print FH "\n\n#endif\n";

close FH;
print "$scriptName - $guard - $fn .....OK!\n";
